package com.dream.testtubebaby_doctor;

import java.io.IOException;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.dream.testtubebaby_doctor.adapter.ProductListConfmtCenterAdapter;
import com.dream.testtubebaby_doctor.data.product.ConfmtCenterListItemData;
import com.dream.testtubebaby_doctor.net.ServerStub;
import com.dream.testtubebaby_doctor.okhttp.OkHttpClientManager;
import com.dream.testtubebaby_doctor.okhttp.OkHttpClientManager.Param;
import com.dream.testtubebaby_doctor.utils.AppUtil;
import com.dream.testtubebaby_doctor.utils.NetUtil;

public class ProductConfmtCenterListActivity extends ProductListBaseActivity {
	protected ArrayList<ConfmtCenterListItemData> mProductListItemDatas = new ArrayList<ConfmtCenterListItemData>();
	private ArrayList<ConfmtCenterListItemData> mProductListItemDatasTemp = new ArrayList<ConfmtCenterListItemData>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initTitleView();
		product_listview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(ProductConfmtCenterListActivity.this, ProductConfmtCenterDetailActivity.class);
				long product_id = mProductListItemDatas.get(position - 1).getId();
				intent.putExtra(ProductConfmtCenterDetailActivity.PRODUCT_DETAIL_ID, product_id);
				startActivity(intent);
			}

		});
	}

	private void initTitleView() {
		TextView top_title_tv = (TextView) findViewById(R.id.top_title_tv);
		top_title_tv.setText(getResources().getString(R.string.product_confmtcenter_title));
	}

	@Override
	protected ProductListConfmtCenterAdapter getAdapter() {
		return new ProductListConfmtCenterAdapter(this, mProductListItemDatas,
				R.layout.product_confmtcenter_item_view);
	}

	@Override
	protected void updateAdapter() {
		mProductListItemDatas.clear();
		mProductListItemDatas.addAll(mProductListItemDatasTemp);
		mHandler.sendEmptyMessage(HANDLER_REFREASH);
	}

	@Override
	protected void moreUpdateAdapter() {
		mProductListItemDatas.addAll(mProductListItemDatasTemp);
		mHandler.sendEmptyMessage(HANDLER_REFREASH);
	}

	protected int loadMoreData() {
		if (NetUtil.checkNet(this)) {
			try {
				ArrayList<ConfmtCenterListItemData> mDatas = downLoadConfmtCenterListItem(fromid);
				mProductListItemDatasTemp.clear();
				if (mDatas != null && mDatas.size() > 0) {
					mProductListItemDatasTemp.addAll(mDatas);
					fromid = mDatas.get(mDatas.size() - 1).getId();
				}
				AppUtil.setRefreashTime(this, 0);
				return MORE_REFREASH_OK;
			} catch (Exception e) {
				e.printStackTrace();
				return TIP_ERROR_SERVER;
			}
		} else {
			return TIP_ERROR_NO_NETWORK;
		}
	}

	protected int refreashData() {
		if (NetUtil.checkNet(this)) {
			try {
				fromid = 0;
				ArrayList<ConfmtCenterListItemData> mDatas = downLoadConfmtCenterListItem(fromid);
				if (mDatas != null && mDatas.size() > 0) {
					mProductListItemDatasTemp.clear();
					mProductListItemDatasTemp.addAll(mDatas);
					fromid = mDatas.get(mDatas.size() - 1).getId();
				}
				AppUtil.setRefreashTime(this, 0);
				return UPDATE_REFREASH_OK;
			} catch (Exception e) {
				e.printStackTrace();
				return TIP_ERROR_SERVER;
			}
		} else {
			return TIP_ERROR_NO_NETWORK;
		}
	}

	private ArrayList<ConfmtCenterListItemData> downLoadConfmtCenterListItem(
			long id) {
		ArrayList<ConfmtCenterListItemData> mdatas = new ArrayList<ConfmtCenterListItemData>();
		Param[] params = new Param[2];
		params[0] = new Param("step", String.valueOf(download_step));
		params[1] = new Param("fromid", String.valueOf(id));
		try {
			String content = OkHttpClientManager.postAsString(
					OkHttpClientManager.BASEURL + "m=Home&c=App&a=GetPNCareCenterList",
					params);//http://121.40.78.46/tb/index.php?m=Home&c=App&a=GetPNCareCenterList&step=10&fromid=0
			JSONObject obj = new JSONObject(content);
			if (obj != null) {
				int result = obj.getInt("result");
				if (result == 0) {
					JSONArray jsonProduct = new JSONArray(
							obj.getString("CareCenterList"));
					for (int i = 0; i < jsonProduct.length(); i++) {
						JSONObject jsonObject = (JSONObject) jsonProduct.get(i);
						ConfmtCenterListItemData itemData = ConfmtCenterListItemData
								.getProductListItemDataNode(jsonObject);
						mdatas.add(itemData);
					}
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return mdatas;

	}
}
